package com.it2297.view;

import javax.swing.JPanel;

import javax.swing.JButton;

import com.alee.extended.date.DateSelectionListener;
import com.alee.extended.date.WebDateField;
import com.it2297.controller.NeedyController;
import com.it2297.controller.ProductController;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.DefaultComboBoxModel;
import javax.swing.SpinnerNumberModel;

public class NeedyView extends JPanel {
	private final NeedyController pc = new NeedyController(this);
	private final JLabel lblNric = new JLabel("NRIC ");
	private final JLabel lblName = new JLabel("Name");
	private final JLabel lblPhone = new JLabel("Phone");
	private final JLabel lblGender = new JLabel("Gender");
	private final JTextField NameTxt = new JTextField();
	private final JTextField PhoneTxt = new JTextField();
	private final JLabel lblAddress = new JLabel("Address");
	private final JTextArea AddressTxtPane = new JTextArea();
	private final JLabel lblEmail = new JLabel("Email");
	private final JTextField EmailTxt = new JTextField();
	private final JLabel lblRace = new JLabel("Race");
	private final JLabel lblNewLabel_5 = new JLabel(
			"Create Needy Resident Personal Record ");
	private final JTextField NricTxt = new JTextField();
	private final JLabel lblDateOfBirth = new JLabel("Date Of Birth");
	private final JLabel lblStatus = new JLabel("Status");
	private final JLabel lblCountryOfBirth = new JLabel("Country Of Birth");
	private final JTextField StatusTxt = new JTextField();
	private final JLabel lblHouseholdIncome = new JLabel("HouseHold Income");
	private final JLabel lblNewLabel_10 = new JLabel("Number Of Dependent");
	private final JLabel lblNewLabel_11 = new JLabel("New label");
	private final JLabel lblNewLabel_12 = new JLabel("Family Size");
	private final JTextField HouseholdIncomeTxt = new JTextField();
	private final JTextField NumOfDependentTxt = new JTextField();
	private final JTextField FamilySizeTxt = new JTextField();
	private final JComboBox comboBoxRace = new JComboBox();
	private final JButton btnCreate = new JButton("Create");
	private final JTextField pwdTxt = new JTextField();
	private final JLabel lblPassword = new JLabel("Password");
	private final JComboBox comboBoxGender = new JComboBox();
	private final JComboBox comboBoxCountry = new JComboBox();
	private final String[] year = { "1986", "1987", "1988", "1989", "1990",
			"1991", "1992" };
	//private final WebDateField dateField = new WebDateField();
	//private Date myDate;
	private final JTextField DateTxt = new JTextField();
	private final JLabel lblNewLabel = new JLabel("*Year-Month-Day ");
	private final JLabel lblNewLabel_1 = new JLabel("e.g 1996-09-09");

	// TODO
	/**
	 * Create the panel.
	 */
	public NeedyView() {
		DateTxt.setBounds(309, 356, 171, 29);
		DateTxt.setColumns(10);
		pwdTxt.setBounds(309, 219, 171, 23);
		pwdTxt.setColumns(10);
		FamilySizeTxt.setBounds(309, 529, 171, 23);
		FamilySizeTxt.setColumns(10);
		NumOfDependentTxt.setBounds(309, 495, 171, 24);
		NumOfDependentTxt.setColumns(10);
		HouseholdIncomeTxt.setBounds(309, 461, 171, 24);
		HouseholdIncomeTxt.setColumns(10);
		StatusTxt.setEditable(false);
		StatusTxt.setText("ava");
		StatusTxt.setBounds(309, 396, 171, 23);
		StatusTxt.setColumns(10);
		NricTxt.setBounds(309, 36, 171, 26);
		NricTxt.setColumns(10);
		EmailTxt.setBounds(309, 287, 171, 23);
		EmailTxt.setColumns(10);
		PhoneTxt.setBounds(309, 185, 171, 23);
		PhoneTxt.setColumns(10);
		setLayout(null);
		NameTxt.setBounds(309, 73, 171, 32);

		NameTxt.setColumns(10);
		this.setSize(1000, 700);

		initGUI();
	}

	public String getNeedyNric() {
		return NricTxt.getText().toString();
	}

	public String getNeedyName() {
		return NameTxt.getText();
	}

	public void setNeedyName(String name) {
		NameTxt.setText(name);
	}

	public String getNeedyAddress() {
		return AddressTxtPane.getText();
	}

	public void setNeedyAddress(String address) {
		AddressTxtPane.setText(address);
	}

	public String getNeedyPhone() {
		return PhoneTxt.getText();
	}

	public void setNeedyPhone(String phone) {
		PhoneTxt.setText(phone);
	}

	public String getNeedyPassword() {
		return pwdTxt.getText();
	}

	// password
	public void setNeedyPassword(String password) {
		pwdTxt.setText(password);
	}

	public String getNeedyGender() {
		return (String) comboBoxGender.getSelectedItem();
	}

	public void setNeedyGender(String gender) {
		comboBoxGender.setSelectedItem(gender);
	}

	public String getNeedyEmail() {
		return EmailTxt.getText();
	}

	public void setNeedyEmail(String email) {
		EmailTxt.setText(email);
	}

	public String getNeedyEthnic() {
		return (String) comboBoxRace.getSelectedItem();
	}

	public void setNeedyEthnic(String ethnic) {
		comboBoxRace.setSelectedItem(ethnic);
	}

	// Date Of Birth
	/*
	 * public Date getNeedyDob(){ String temp =
	 * Integer.toString((Integer)spinnerDay.getValue())+
	 * "-"+Integer.toString((Integer)spinnerMonth.getValue())+"-" +(String)
	 * comboBox.getSelectedItem(); SimpleDateFormat formatter = new
	 * SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date =
	 * (Date)formatter.parse(temp); } catch (ParseException e) { // TODO
	 * Auto-generated catch block e.printStackTrace(); }
	 * 
	 * return date; }
	 */

	/*
	 * public String setNeedyDob(String dob){ String date = (String)
	 * spinnerDay.getValue()+ (String) spinnerMonth.getValue()+ (String)
	 * comboBox.getSelectedItem() ; return date; }
	 */
	
	 public Date getNeedyDob(){
		 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
		 
		 Date date = null;
		try {
			date = (Date)formatter.parse(DateTxt.getText());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(date);
		 return date;
		 
	  }
	
	 public void setNeedyDob(Date dob){
		 DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		 String reportDate = df.format(dob);
		 DateTxt.setText(reportDate);
		 
	 }
	 
	

	public String getNeedyStatus() {
		return StatusTxt.getText();
	}

	public void setNeedyStatus(String status) {
		StatusTxt.setText(status);
	}

	//
	public String getNeedyCountryOfBirth() {
		return (String) comboBoxCountry.getSelectedItem();
	}

	public void setNeedyCountryOfBirth(String country) {
		comboBoxCountry.setSelectedItem(country);
	}

	public double getNeedyHouseHoldIncome() {
		return Double.parseDouble(HouseholdIncomeTxt.getText());
	}

	// needy

	public void setNeedyHouseHoldIncome(double householdincome) {
		HouseholdIncomeTxt.setText(Double.toString(householdincome));
	}

	public int getNeedyNumOfDependent() {
		return Integer.parseInt(NumOfDependentTxt.getText());
	}

	public void setNeedyNumOfIndependent(int numofindependent) {
		NumOfDependentTxt.setText(Integer.toString(numofindependent));
	}

	public int getNeedyFamilySize() {
		return Integer.parseInt(FamilySizeTxt.getText());
	}

	public void setNeedyFamilySize(int familysize) {
		FamilySizeTxt.setText(Integer.toString(familysize));
	}

	private void initGUI() {
		setLayout(null);
		lblNric.setFont(new Font("Arial", Font.PLAIN, 19));
		lblNric.setBounds(193, 41, 57, 23);

		add(lblNric);
		lblName.setFont(new Font("Arial", Font.PLAIN, 19));
		lblName.setBounds(193, 75, 51, 23);

		add(lblName);

		add(NameTxt);
		lblPhone.setFont(new Font("Arial", Font.PLAIN, 19));
		lblPhone.setBounds(193, 174, 69, 38);

		add(lblPhone);
		lblGender.setFont(new Font("Arial", Font.PLAIN, 19));
		lblGender.setBounds(193, 242, 69, 38);

		add(lblGender);

		add(PhoneTxt);
		lblAddress.setFont(new Font("Arial", Font.PLAIN, 19));
		lblAddress.setBounds(193, 125, 72, 38);

		add(lblAddress);
		AddressTxtPane.setWrapStyleWord(true);
		AddressTxtPane.setLineWrap(true);
		AddressTxtPane.setBounds(309, 116, 171, 58);

		add(AddressTxtPane);
		lblEmail.setFont(new Font("Arial", Font.PLAIN, 19));
		lblEmail.setBounds(193, 276, 69, 38);

		add(lblEmail);

		add(EmailTxt);
		lblRace.setFont(new Font("Arial", Font.PLAIN, 19));
		lblRace.setBounds(193, 313, 69, 32);

		add(lblRace);
		lblNewLabel_5.setFont(new Font("Arial", Font.PLAIN, 19));
		lblNewLabel_5.setBounds(189, 0, 351, 23);

		add(lblNewLabel_5);

		add(NricTxt);
		lblDateOfBirth.setFont(new Font("Arial", Font.PLAIN, 19));
		lblDateOfBirth.setBounds(174, 356, 109, 35);

		add(lblDateOfBirth);
		lblStatus.setFont(new Font("Arial", Font.PLAIN, 19));
		lblStatus.setBounds(193, 402, 58, 14);

		add(lblStatus);
		lblCountryOfBirth.setFont(new Font("Arial", Font.PLAIN, 19));
		lblCountryOfBirth.setBounds(146, 427, 147, 23);

		add(lblCountryOfBirth);

		add(StatusTxt);
		lblHouseholdIncome.setFont(new Font("Arial", Font.PLAIN, 19));
		lblHouseholdIncome.setBounds(129, 457, 164, 23);

		add(lblHouseholdIncome);
		lblNewLabel_10.setFont(new Font("Arial", Font.PLAIN, 19));
		lblNewLabel_10.setBounds(85, 495, 206, 23);

		add(lblNewLabel_10);
		lblNewLabel_11.setBounds(118, 705, 46, 14);

		add(lblNewLabel_11);
		lblNewLabel_12.setFont(new Font("Arial", Font.PLAIN, 19));
		lblNewLabel_12.setBounds(163, 529, 115, 23);

		add(lblNewLabel_12);

		add(HouseholdIncomeTxt);

		add(NumOfDependentTxt);

		add(FamilySizeTxt);

		comboBoxRace.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				comboBoxRace.getSelectedItem();
				System.out.println(getNeedyEthnic());
			}

		});

		comboBoxRace.setModel(new DefaultComboBoxModel(new String[] {"CHI", "IND", "MAL"}));
		comboBoxRace.setBounds(309, 321, 171, 23);

		add(comboBoxRace);
		btnCreate.setBounds(308, 557, 89, 32);

		add(btnCreate);

		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// createNeedy();

				if (NricTxt.getText().equals("")
						|| NameTxt.getText().equals("")
						|| AddressTxtPane.getText().equals("")
						|| PhoneTxt.getText().equals("")
						|| pwdTxt.getText().equals("")
						|| EmailTxt.getText().equals("")
						|| DateTxt.getText().equals("")
						|| StatusTxt.getText().equals("")
						|| HouseholdIncomeTxt.getText().equals("")
						|| NumOfDependentTxt.getText().equals("")
						|| FamilySizeTxt.getText().equals("")) {
					JOptionPane.showMessageDialog(null,
							"Please fill in all the necessary details.");
				} else {
					

					createNeedy();
					
					NricTxt.setText("");
					NameTxt.setText("");
					AddressTxtPane.setText("");
					PhoneTxt.setText("");
					pwdTxt.setText("");
					EmailTxt.setText("");
					DateTxt.setText("");
					StatusTxt.setText("");
					HouseholdIncomeTxt.setText("");
					NumOfDependentTxt.setText("");
					FamilySizeTxt.setText("");
					
				}

			}
		});

		add(pwdTxt);
		lblPassword.setFont(new Font("Arial", Font.PLAIN, 19));
		lblPassword.setBounds(193, 223, 85, 14);

		add(lblPassword);
		comboBoxGender.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				comboBoxGender.getSelectedItem();
				System.out.println(getNeedyGender());

			}
		});
		comboBoxGender.setModel(new DefaultComboBoxModel(new String[] {
				"Female", "Male", "Others" }));
		comboBoxGender.setBounds(309, 253, 171, 23);

		add(comboBoxGender);
		comboBoxCountry.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				comboBoxCountry.getSelectedItem();
				System.out.println(getNeedyCountryOfBirth());

			}
		});
		comboBoxCountry.setModel(new DefaultComboBoxModel(new String[] { "SGP",
				"MYS", "IND", "THA" }));
		comboBoxCountry.setBounds(309, 430, 171, 20);

		add(comboBoxCountry);
		
		
	 
	
		/*
		add(dateField);
		dateField.setDateFormat(new SimpleDateFormat("yyyy-mm-dd"));
		dateField.setBounds(0, 0, 200, 40);
		dateField.addDateSelectionListener(new DateSelectionListener() {

			@Override
			public void dateSelected(Date arg0) {
				myDate = arg0;
			}

		});
*/
		
		add(DateTxt);
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 9));
		lblNewLabel.setBounds(484, 356, 95, 14);
		
		add(lblNewLabel);
		lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 9));
		lblNewLabel_1.setBounds(484, 369, 75, 14);
		
		add(lblNewLabel_1);
	}

	
	private void createNeedy() {
		pc.CreateNeedy();
	}
}
